
[HEADER]
	VERSION		01.00.00.00
	DESCRIPTION Iridescence Example
	COPYRIGHT	Imagination Technologies Ltd.
[/HEADER]

[TEXTURES]
	FILE thickness 	Thickness.pvr		LINEAR-LINEAR-NEAREST
[/TEXTURES]

[VERTEXSHADER] 
	NAME 		MyVertexShader 

	[GLSL_CODE]
		attribute vec4  inVertex;
		attribute vec3  inNormal;
		attribute vec2  inTexCoord;
		
		uniform mat4  MVPMatrix;
		uniform vec3  LightPosition;
		uniform vec3  EyePosition;
		
		varying float  CosViewAngle;
		varying float  LightIntensity;
		varying vec2   TexCoord;
		
		void main()
		{
			gl_Position = MVPMatrix * inVertex;
			
			vec3 LightDir = normalize(LightPosition - inVertex.xyz);
			
			vec3 eyeDirection = normalize(EyePosition - inVertex.xyz);
			
			// Simple diffuse lighting 
			LightIntensity = max(dot(LightDir, inNormal), 0.0);
		
			// Cosine of the angle between surface normal and eye direction
			// We clamp at 0.1 to avoid ugly aliasing at near 90° angles
			CosViewAngle = max(dot(eyeDirection, inNormal), 0.1);
			
			TexCoord = inTexCoord;
		}
	[/GLSL_CODE]
[/VERTEXSHADER]
    
[FRAGMENTSHADER] 
	NAME 		MyFragmentShader 

	[GLSL_CODE]
		uniform sampler2D  sThicknessTex;
		
		uniform float  MinThickness;
		uniform float  MaxVariation;
		
		varying float  CosViewAngle;
		varying float  LightIntensity;
		varying vec2   TexCoord;
		
		// We use wave numbers (k) for the iridescence effect, given as
		//   k =  2 * pi / wavelength in nm.
		const float  PI = 3.141592654;
		const vec3   cRgbK = 2.0 * PI * vec3(1.0/475.0, 1.0/510.0, 1.0/650.0);
		
		void main()
		{
			float thickness = texture2D(sThicknessTex, TexCoord).r * MaxVariation + MinThickness;
			float delta = (thickness / LightIntensity) + (thickness / CosViewAngle);
			vec3 color = cos(delta * cRgbK) * LightIntensity;
			gl_FragColor = vec4(color, 1.0);
		}
	[/GLSL_CODE]
[/FRAGMENTSHADER]
 
[EFFECT] 
	NAME 	Iridescence

	UNIFORM	MVPMatrix		WORLDVIEWPROJECTION
	UNIFORM LightPosition	LIGHTPOSWORLD0
	UNIFORM EyePosition		EYEPOSMODEL

	UNIFORM MinThickness	MINTHICKNESS	float(100.0)
	UNIFORM MaxVariation	MAXVARIATION	float(100.0)

	ATTRIBUTE	inVertex	POSITION
	ATTRIBUTE	inNormal	NORMAL
	ATTRIBUTE	inTexCoord	UV0

	VERTEXSHADER MyVertexShader
	FRAGMENTSHADER MyFragmentShader
	
	TEXTURE 0 thickness

[/EFFECT]
